package huanzhao.erp.util;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 图片验证码工具类
 *
 * @author April
 * @version 1.0
 */
@Slf4j
public class ValidateCodeUtil {
    private static final Random random = new Random();
    private final static String sessionKey = "JCCODE";
    private final static int width = 160; //验证码的宽
    private final static int height = 45; //验证码的高
    private final static int randomStrNum = 4; //验证码字符个数
    private final static String randomString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWSYZ";
    private final static Font font = new Font("Times New Roman", Font.PLAIN, 40);

    //颜色的设置
    private static Color getRandomColor(int fc, int bc) {
        fc = Math.min(fc, 255);
        bc = Math.min(bc, 255);
        int r = fc + random.nextInt(bc - fc - 16);
        int g = fc + random.nextInt(bc - fc - 14);
        int b = fc + random.nextInt(bc - fc - 12);
        return new Color(r, g, b);
    }


    //干扰线的绘制
    private static void drawLine(Graphics g) {
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        int xl = random.nextInt(20);
        int yl = random.nextInt(10);
        g.drawLine(x, y, x + xl, y + yl);
    }

    //随机字符的获取
    private static String getRandomString(int num) {
        num = num > 0 ? num : randomString.length();
        return String.valueOf(randomString.charAt(random.nextInt(num)));
    }

    //字符串的绘制
    private static String drawString(Graphics g, String randomStr, int i) {
        g.setFont(font);
        g.setColor(getRandomColor(108, 190));
        String rand = getRandomString(random.nextInt(randomString.length()));
        randomStr += rand;
        g.translate(random.nextInt(3), random.nextInt(6));
        g.drawString(rand, 40 * i + 10, 25);
        return randomStr;
    }


    /**
     * 生成随机图片验证码
     *
     * @param session  用于存储验证码的session
     * @param response 请求的响应，用于返回图片
     * 2024/6/28 下午4:54:28
     */
    public static void getRandomCodeImage(HttpSession session, HttpServletResponse response) {
        // BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        Graphics g = image.getGraphics();
        g.fillRect(0, 0, width, height);
        g.setColor(getRandomColor(105, 189));
        g.setFont(font);
        // 干扰线
        //验证码中夹杂的干扰线数量
        int lineSize = 30;
        for (int i = 0; i < lineSize; i++) {
            drawLine(g);
        }
        // 随机字符
        String randomStr = "";
        for (int i = 0; i < randomStrNum; i++) {
            randomStr = drawString(g, randomStr, i);
        }
        // System.out.println("随机字符：" + randomStr);
        g.dispose();
        // 移除之前的session中的验证码信息
        session.removeAttribute(sessionKey);
        // 重新将验证码放入session
        session.setAttribute(sessionKey, randomStr);
        try {
            // 将图片以png格式返回,返回的是图片
            ImageIO.write(image, "PNG", response.getOutputStream());
        } catch (Exception e) {
            log.error("生成图片验证码错误：", e);
        }
    }
}
 
 
 
 